package com.xiyou.luckdraw.api.service.impl;

import ch.qos.logback.core.util.FileUtil;
import com.xiyou.luckdraw.api.dto.upload.UploadRequest;
import com.xiyou.luckdraw.api.dto.upload.UploadResponse;
import com.xiyou.luckdraw.api.service.IUploadService;
import com.xiyou.luckdraw.common.constants.ResultCodeConstants;
import com.xiyou.luckdraw.common.enums.OssDirEnum;
import com.xiyou.luckdraw.common.exception.ExceptionProcessorUtils;
import com.xiyou.luckdraw.common.utils.FileUtils;
import com.xiyou.luckdraw.common.utils.OssUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @ClassName: UploadServiceImpl
 * @Description:
 * @author: limingxing
 * @Date: 2019-10-10 19:40
 */
@Slf4j
@Service
public class UploadServiceImpl implements IUploadService {


    @Autowired
    private OssUtils ossUtils;

    @Override
    public UploadResponse upload(UploadRequest request) {
        log.error("Begin UploadServiceImpl.upload request : {}", request);
        UploadResponse response = new UploadResponse();
        try {
            if(null != request.getFile()){
                String pic = ossUtils.upload(OssDirEnum.notice, FileUtils.getUuidFileName(request.getFile().getOriginalFilename()), request.getFile().getInputStream());
                response.setUrl(pic);
            }
            response.setCode(ResultCodeConstants.SUCCESS.getCode());
            response.setMsg(ResultCodeConstants.SUCCESS.getMessage());
        } catch (IOException e) {
            log.error("UploadServiceImpl.upload Occur Exception : {}", e);
            ExceptionProcessorUtils.wrapperHandlerException(response, e);
        }
        return response;
    }
}
